<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" destroy-on-close @closed="$emit('closed')" :close-on-press-escape="mode != 'show' ? false : true" fullscreen>
        <el-container>
            <el-header>
                <div class="left-panel">
                    <div style="font-size: 16px;">选择打印模板：</div>
                    <el-select v-model="form.template_id" clearable filterable placeholder="请选择模板" @change="templateChange">
                        <el-option v-for="item in templateList" :key="item.id" :label="item.name" :value="item.id"/>
                    </el-select>
                </div>
                <div class="right-panel">
                    <div class="right-panel-search">
                        <el-button type="success" plain @click="toPrint">打印</el-button>
                        <el-button type="success" plain @click="preview">预览</el-button>
                        <el-button type="success" plain @click="pdf">下载PDF</el-button>
                    </div>
                </div>
            </el-header>
            <el-main style="height: 810px;overflow: auto;overflow-x: hidden;">
                <sc-editor-print ref="editor" v-model="content" placeholder="请输入"></sc-editor-print>
            </el-main>
        </el-container>
        <el-footer style="text-align: right;position: fixed;bottom: 0;right: 0;left: 0;padding: 10px 20px;background: #fff;border-top: 1px solid #eee;">
        <el-button @click="visible = false">取 消</el-button>
        </el-footer>
    </el-dialog>
</template>

<script>
import tinymce from 'tinymce/tinymce'
import store from "@/store";
// import { saveAs as FileSaver } from 'file-saver'
export default {
    emits: ['success', 'closed'],
    data() {
        return {
            dialog: {
                client: false
            },
            mode: "add",
            titleMap: {
                add: '打印',
                edit: '打印',
                show: '打印'
            },
            visible: false,
            isSaveing: false,
            form: {
                id: 1,
                template_id: '',
            },
            templateList: [],
            MenuName: '',
            content:''
        }
    },
    mounted() {
        let MenuName = store.state.print.MenuName
        if (MenuName) {
            this.MenuName = MenuName
        }
        this.getTemplate()
    },
    beforeUnmount() {
        console.log('销毁')
        tinymce.remove()
    },
    methods: {
        //显示
        open(mode = 'add') {
            this.mode = mode;
            this.visible = true;
            return this
        },
        /**模板选中事件*/
        templateChange() {
            this.setData()
        },
        /**获取打印模板*/
        async getTemplate() {
            var res = await this.$API.print.template.all.get({MenuName: this.MenuName});
            this.templateList = res.data
            if (this.templateList.length > 0) {
                this.form.template_id = this.templateList[0].id
                this.setData()
            }
        },
        //表单注入数据
        async setData() {
            var res = await this.$API.print.template.printContant.get(this.form);
            this.content = res.data.content
            this.handleContent()
        },
        /**获取打印数据id*/
        getId(data) {
            this.form.id = data
        },
        /**打印按钮点击*/
        toPrint() {
            tinymce.activeEditor.execCommand('mceprint');
        },
        /**预览*/
        preview() {
            tinymce.activeEditor.execCommand('mcePreview');
        },
        /**下载pdf点击*/
        async pdf() {
            let obj = {
                export: 1,
                ...this.form
            }
            var res = await this.$API.print.template.printContant.get(obj);
            if (res.code == 1) {
                // const that = this
                // var oReq = new XMLHttpRequest()
                // var URL= res.data.pdf_url // URL 为URL地址
                // oReq.open('GET', URL, true)
                // oReq.responseType = 'blob'
                // oReq.onload = function() {
                // var file = new Blob([oReq.response], {
                //     type: 'blob'
                // })
                // FileSaver.saveAs(file, that.name) // that.name为文件名
                // }
                // oReq.send()
                const fileUrl = res.data.pdf_url;
                window.open(fileUrl, '_blank')
                // const link = document.createElement("a");
                // link.href = fileUrl;
                // link.download = fileUrl;
                // link.click();
            } else {
                this.$alert(res.message, "提示", {type: "error"});
            }
            console.log(res);
        },
        handleContent() {
            tinymce.activeEditor.setContent(this.content)
            this.$nextTick(() => {
                if (this.$refs.editor && this.$refs.editor.EditorOnsize) {
                this.$refs.editor.EditorOnsize()
                }
            })
        },
    }
}
</script>

<style lang="scss">
.el-input-group__append {
  padding: 0 8px;
}
</style>
